var ctx;

function setCtx(ctx){
	this.ctx = ctx;
	initListener();
}

function ifNull(mess){
	if(mess == null || mess == undefined){
		mess = "";
	}
	return mess;
}

var roleType = $("#roleType").val();
if(roleType == -1 ){
	$("#functionArea").hide();
	$("#cellArea").hide();
}else if(roleType == 0 ){
	$("#functionArea").show();
	$("#cellArea").hide();
}else{
	$("#functionArea").show();
	$("#cellArea").show();
}

//编辑或新增区域等级下拉框
function initListener(){
	$('#roleType').change(function(){ 
		roleType = $(this).children('option:selected').val();
		
		if(roleType == -1 ){
			$("#functionArea").hide();
			$("#cellArea").hide();
		}else if(roleType == 0 ){
			$("#functionArea").show();
			$("#cellArea").hide();
		}else{
			$("#functionArea").show();
			$("#cellArea").show();
		}
		
		parent.$('#selectRm').val('');
		parent.$('#selectedCellId').val('');
	});
}


//验证区域名称不能为空
function checkRoleName(){
	 var roleName = $("#roleName").val();
	//解决ie9以下placeholder与value重复的问题
		if(roleName == $("#roleName").attr("placeholder")){
			roleName = "";
		}
		if(roleName == null || roleName == ""){
			layer.tips('角色名不能为空', '#roleName', {
			    tips: 3,
			    time:1000
			});
			return false;
		}else{
			return true;
	}
}

//验证区域等级
function checkRoleType(){
	var roleType = $("#roleType").val();
	if(roleType == -1){
		layer.tips('请选择角色类型!', '#editLevel', {
		    tips: 3,
		    time:1000
		});
		return false;
	}else{
		return true;
	}
}

var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
function saveOrUpdate(){
	var id = 0;
	if($("#id").val() != null && $("#id").val() != ""){
		id = $("#id").val();
	}
	var roleName = $("#roleName").val();
	var descrip = $("#descrip").val();
	//解决ie9以下placeholder与value重复的问题
	if(roleName == $("#roleName").attr("placeholder")){
		roleName = "";
	}
	if(descrip == $("#descrip").attr("placeholder")){
		descrip = "";
	}

	var roleType = $("#roleType").val();
	
	var menus = parent.$('#selectRm').val();
	var cells = parent.$('#selectedCellId').val();
	
	if(roleType == 0){
		cells = "";
	}else if(roleType != -1 && (cells == null || cells == '' || cells == '0')){
		layer.tips('请选择小区', '#cells', {
		    tips: 3,
		    time:1000
		});
		return;
	}
	
	if(roleType != -1 && (menus == null || menus == '' || menus == '0')){
		layer.tips('请选择功能', '#menus', {
		    tips: 3,
		    time:1000
		});
		return;
	}
	

		if(checkRoleName() && checkRoleType()){
			layer.load(1);
			$.post(ctx+"/role/editRole",{
				rname:roleName,
				rdesc:descrip,
				roleType:roleType,
				rid:id,
				menus : menus,
				cells : cells
					},function(data){
						data = JSON.parse(data);
						layer.closeAll('loading');
						
						if(data.success){
							//修改的角色就是当前用户的角色
		    				if(data.obj != null && data.obj == id){
		    					parent.layer.alert('当前角色权限已被修改，必须重新登陆!', {
		    					    skin: 'layui-layer-molv' //样式类名
		    					    ,closeBtn: 0
		    					}, function(index){
		    						$.get(ctx + "/exist",{},function(data){
		    							data = JSON.parse(data);
		    							if(data.success){
		    								parent.location.href = ctx;
		    							}
		    						});
		    					});
		    				}else{
		    					layer.msg("操作成功",{
								    icon: 1,
								    time: 1000 //2秒关闭（如果不配置，默认是3秒）
								},function(){
									parent.loadContent("/role/roleList?type=" + roleType);
									parent.layer.close(index); //根据窗口索引关闭窗口   
								});
		    				}
						} else {
							layer.msg(data.msg,{
							    icon: 2,
							    time: 1000 //2秒关闭（如果不配置，默认是3秒）
							});
						}
					});
			}
}

function setRoleMenu (rId){
	var type = $("#roleType").children('option:selected').val();
	var cell = parent.$('#selectedCellId').val();
	var menus = parent.$('#selectRm').val();
	
	if(type != 0 && (cell == null || cell == '' || cell == '0')){
		layer.tips('请先选择小区', '#cells', {
		    tips: 3,
		    time:1000
		});
		return;
	}
	
	parent.saveOrUpdateRoleMenu("功能权限",ctx + "/role/getRMenu?roleId=" + (rId == null || rId == '' ? "0" : rId)
			+ "&type=" + type+ "&selectcells=" + cell+ "&menus=" + menus);
}

function chooseCell(rId){
	var cell = parent.$('#selectedCellId').val();
	parent.saveOrUpdateRoleCell("选择小区",ctx + "/cellInfo/queryAllCellInfo?rId=" + (rId == null || rId == '' ? "0" : rId)
			+ "&cells=" + cell);
}
 